<?php
namespace App\Service\OrderService\OrderInterfaceService;
use Service\BaseService;
use Service\Code\Normal;
use App\Model\PssModel\OrderRefund;
use App\Model\PssModel\OrderRefundLog;
use App\Service\OrderService\OrderRefundService\OrderRefundMessage;
use App\Model\PssModel\OrderSupplier;

/**
 * BBC同步云仓数据物流发货状态
 * 1、退货退款物流信息
 * 2、换货物流信息
 */
class OrderNotifyUpstreamExpress extends BaseService
{
    /**
     * 退换货物流
     */
    public function index( array $param = [] )
    {
        $binfo  = $param['binfo'];
        $bid    = $binfo['bid'];
        $requestData = $param['requestData'];
        if( empty($requestData['order_sn']) ) {
            return Normal::returnCode(Normal::FAIL,"售后订单号order_sn不能为空");
        }
        if( empty($requestData['order_refund_id']) ) {
            return Normal::returnCode(Normal::FAIL,"售后单号order_refund_id不能为空");
        }
        if( empty($requestData['refund_type']) ) {
            return Normal::returnCode(Normal::FAIL,"售后方式refund_type不能为空");
        }
        
        //当前类型不支持
        $refund_type = $requestData['refund_type'];
        $order_sn    = $requestData['order_sn'];
        $refundId    = $requestData['order_refund_id'];
        if( !in_array($refund_type,[OrderRefundMessage::refund_type_refundchangegoods,OrderRefundMessage::refund_type_goodsandmoney]) ) {
            return Normal::returnCode(Normal::FAIL,"售后方式refund_type仅支持换货，退货退款，其它类型不支持触发当前业务!");
        }
       
        if( empty($requestData['change_express_sn']) ) {
            return Normal::returnCode(Normal::FAIL,"消费者发往仓库-换货/补发运单号change_express_sn不能为空");
        }
        
        if( empty($requestData['change_express_name']) ) {
            return Normal::returnCode(Normal::FAIL,"消费者发往仓库-换货/补发运单物流公司change_express_name不能为空");
        }
        
        if( empty($requestData['change_express_time']) ) {
            return Normal::returnCode(Normal::FAIL,"换货/补发运单号（仓库发往消费者）时间change_express_time不能为空");
        }
        
        if( empty($requestData['create_user_id']) ) {
            return Normal::returnCode(Normal::FAIL,"create_user_id不能为空");
        }
        
        if( empty($requestData['create_user_name']) ) {
            return Normal::returnCode(Normal::FAIL,"create_user_name不能为空");
        }
                
        $orderRefund = OrderRefund::instance()->info(['id'=>$refundId,'bid'=>$bid,'order_sn'=>$order_sn]);
        if( empty($orderRefund)) {
            return Normal::returnCode(Normal::FAIL,"售后申请订单记录不存在，请联系管理员进行核实处理");
        }
        
        $order_refund_id            = $orderRefund['id'];
        $change_express_sn          = $requestData['change_express_sn'];
        $change_express_name        = $requestData['change_express_name'];
        $change_express_time        = $requestData['change_express_time'];
        $refund_type_name           = OrderRefund::instance()->refund_type[$refund_type];
        
        //原始订单信息
        $supplierOrder = OrderSupplier::instance()->info(['id'=>$orderRefund['sub_order_id']]);
        if( empty($supplierOrder)) {
            return Normal::returnCode(Normal::FAIL,"原始供应商订单数据不存在，请联系管理员进行核实处理");
        }
        
        try
        {
            OrderRefund::instance()->startTrans();
            $updateExpressArr = [
                'change_express_sn'     => $change_express_sn,
                'change_express_name'   => $change_express_name,
                'change_express_time'   => $change_express_time,
                'md'                    => time(),
            ];
            
            if( $refund_type == OrderRefundMessage::refund_type_goodsandmoney ) {//退款退货
                $_where_refund_status = ['5'];
            } else {//换货
                $_where_refund_status = ['7'];
            }
            //用户填了退货/换货物流后 更新状态
            if(in_array($refund_type, [OrderRefundMessage::refund_type_goodsandmoney, OrderRefundMessage::refund_type_refundchangegoods])){
                // 9-买家已退货，待确认收货
                $updateExpressArr['refund_status'] = 9;
            }
            //当前售后订单状态
            $refund_status_name = OrderRefund::instance()->refund_status[$orderRefund['refund_status']];
            //物流发货状态修改
            $result = OrderRefund::instance()
            ->where('id',$order_refund_id)
            ->whereIn('refund_status',$_where_refund_status)
            ->update($updateExpressArr);
            if( !$result ) {
                OrderRefund::instance()->rollback();
                return Normal::returnCode(Normal::FAIL,"当前售后订单状态[{$refund_status_name}]节点不正确,请联系管理员进行核实处理！");
            }
            
            //写入订单日志
            OrderRefundLog::instance()->addLog($order_refund_id,"用户发起订单消息-[{$refund_type_name}]", $requestData['create_user_id'], $requestData['create_user_name']);
            OrderRefund::instance()->commit();
            return Normal::returnCode(Normal::SUC,"退换货物流接收成功");
        } 
        catch ( \Throwable $th) 
        {
            OrderRefund::instance()->rollback();
            return Normal::returnCode(Normal::FAIL,"退换货物流处理失败:{$th->getMessage()}，请联系管理员进行核实处理");
        }
    }
}